<template>
  <div id="app">
    <i v-show="isShow">
      <router-view></router-view>
    </i>
  </div>
</template>

<script>
import { dingTalkPc } from "./auth/dingtalk-auth";
export default {
  name: "app",
  data: function() {
    return {
      isShow: false
    };
  },
  methods: {
    // 钉钉签名，免登调用
    authSerivce: function() {
      //JSAPILIST功能注册
      let ddjsApi = [
        "device.notification.alert",
        "device.notification.confirm",
        "runtime.permission.requestOperateAuthCode",
        "biz.util.uploadImage",
        "biz.contact.choose",
        "biz.util.share",
        "biz.ding.post",
        "biz.user.get"
      ];

      return new Promise(function(resolve, reject) {
        dingTalkPc.ddConfig(ddjsApi, "api/auth/getPcJsConfig").then(resCorpid => {
          console.log('resCorpid_______________________',resCorpid)
            localStorage.setItem("corpId", resCorpid);
            dingTalkPc.authLogin(resCorpid,"api/auth/login" ).then(token => {
                //设置HTTP.Header值
                console.log(token,'---------')
                localStorage.setItem("token", token.token);
                sessionStorage.setItem('loginUser',JSON.stringify(token.user))
                resolve(1);
              });
          },err => {
            console.log("签名报错啦！");
          }
        );
      });
    },
    message: function(msg, type) {
        this.$message({
            message: msg,
            type: type
        });
    }
  },

  created: function() {
        // this.isShow = true ;
    //钉钉软件内路由跳转
    //钉钉内首页路由跳转，签名，免登；
    this.authSerivce().then(res => {
      let loginUser = sessionStorage.getItem('loginUser');
      if(res === 1 && loginUser){
        if(JSON.parse(loginUser).userId == '01020868096941'){
          this.isShow = true ;
        }else{
          this.message("您没有权限访问该应用","error");
        }
      }
    });
  }
};
</script>
